package name.fowler.refactoring.simplestore.domain;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import java.util.ArrayList;
import java.util.List;

@Entity
@NamedQuery(name = "customer.list", query = "select c from Customer c")
public class Customer extends JPAEntity{
	private String name;
    private String nickName = "noNickName";
	
	//TODO Create ManyToMany relations
	
    @OneToMany(cascade= CascadeType.ALL, orphanRemoval = true)
    private List<Rental> rentals = new ArrayList<Rental>();

	public Customer(String name) {
		this.name = name;
	}

    protected Customer() {
    }

    public String getName() {
		return name;
	}


	public void addRental(Movie movie, int daysRented){
        Rental arg = new Rental(movie, daysRented);
        rentals.add(arg);
	}

    public int getFrequentRenterPoints() {
        int frequentRenterPoints = 0;
        for(Rental each: rentals) {
            frequentRenterPoints += each.getRentalFrequentPoints();
        }
        return  frequentRenterPoints;
    }

    public List<Rental> getRentals() {
        return rentals;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
}

